﻿using System.Text;

namespace RaceList.Admin.Relay
{
    public partial class Paging : System.Web.UI.UserControl
    {
        /// <summary>
        /// 数据总条数
        /// </summary>
        public int TotalNum = 0;
        /// <summary>
        /// 分页个数
        /// </summary>
        public int PageSize = 10;
        /// <summary>
        /// 表示显示出的页数数量
        /// </summary>
        public int PageCount = 7;
        /// <summary>
        /// 当前页数
        /// </summary>
        public int CurrentPage = 1;
        /// <summary>
        /// 绑定分页
        /// </summary>
        public void Paging_Bind()
        {
            StringBuilder sb = new StringBuilder();
            //总页数
            int AllPage = TotalNum / PageSize + (TotalNum % PageSize > 0 ? 1 : 0);
            //中间数
            int MidNum = PageCount / 2 + 1;
            if (AllPage == 0)
            {
                TextInfo.InnerHtml = "共0条&nbsp;&nbsp;当前第0/0页&nbsp;&nbsp;每页"+PageSize+"条&nbsp;&nbsp;";
            }
            else {
                TextInfo.InnerHtml = "共" + TotalNum.ToString() + "条&nbsp;&nbsp;当前第" + CurrentPage.ToString() + "/" + AllPage + "页&nbsp;&nbsp;每页" + PageSize + "条&nbsp;&nbsp;";
            }
            
            if (CurrentPage != 1&&AllPage!=0)
            {
                sb.Append("<a href=\"javascript:__doPostBack('ctl00$MyContentPlaceHolder$btnPage','1')\" style=\"text-decoration:none;\"> 首页&nbsp;</a>");
                sb.Append("<a href=\"javascript:__doPostBack('ctl00$MyContentPlaceHolder$btnPage','"+(CurrentPage-1)+"')\" style=\"text-decoration:none;\"> 上一页&nbsp;</a>");
            }
            else if(AllPage!=0)
            {
                sb.Append("<a href=\"javascript:void(0);\" style=\"text-decoration:none;\"> 首页&nbsp;</a>");
                sb.Append("<a href=\"javascript:void(0);\" style=\"text-decoration:none;\"> 上一页&nbsp;</a>");
            }
            if (PageCount >= AllPage)
            {
                for (var i = 1; i <= AllPage; i++)
                {
                    if (i == CurrentPage)
                    {
                        sb.Append("<a href=\"javascript:void(0);\" style=\"text-decoration:none; color:red;\" > [" + i + "]&nbsp;</a>");
                    }
                    else
                    {
                        sb.Append("<a href=\"javascript:__doPostBack('ctl00$MyContentPlaceHolder$btnPage','" + i + "')\" style=\"text-decoration:none;\"> [" + i + "]&nbsp;</a>");
                    }
                }
            }
            else
            {
                if (CurrentPage <= MidNum + 1)
                {
                    for (var i = 1; i <= PageCount; i++)
                    {
                        if (i == CurrentPage)
                        {
                            sb.Append("<a href=\"javascript:void(0);\" style=\"text-decoration:none; color:red;\" > [" + i + "]&nbsp;</a>");
                        }
                        else
                        {
                            sb.Append("<a href=\"javascript:__doPostBack('ctl00$MyContentPlaceHolder$btnPage','" + i + "')\" style=\"text-decoration:none;\"> [" + i + "]&nbsp;</a>");
                        }
                    }
                }
                else
                {
                    if (MidNum + 1 >= AllPage - CurrentPage)
                    {
                        for (var i = AllPage - PageCount + 1; i <= AllPage; i++)
                        {
                            if (i == CurrentPage)
                            {
                                sb.Append("<a href=\"javascript:void(0);\" style=\"text-decoration:none; color:red;\" > [" + i + "]&nbsp;</a>");
                            }
                            else
                            {
                                sb.Append("<a href=\"javascript:__doPostBack('ctl00$MyContentPlaceHolder$btnPage','" + i + "')\" style=\"text-decoration:none;\"> [" + i + "]&nbsp;</a>");
                            }
                        }
                    }
                    else
                    {
                        for (var i = CurrentPage - MidNum; i <= CurrentPage + MidNum; i++)
                        {
                            if (i == CurrentPage)
                            {
                                sb.Append("<a href=\"javascript:void(0);\" style=\"text-decoration:none; color:red;\" > [" + i + "]&nbsp;</a>");
                            }
                            else
                            {
                                sb.Append("<a href=\"javascript:__doPostBack('ctl00$MyContentPlaceHolder$btnPage','" + i + "')\" style=\"text-decoration:none;\"> [" + i + "]&nbsp;</a>");
                            }
                        }
                    }
                }
            }
            if (CurrentPage != AllPage&&AllPage!=0)
            {
                sb.Append("<a href=\"javascript:__doPostBack('ctl00$MyContentPlaceHolder$btnPage','" + (CurrentPage + 1) + "')\" style=\"text-decoration:none;\"> 下一页&nbsp;</a>");
                sb.Append("<a href=\"javascript:__doPostBack('ctl00$MyContentPlaceHolder$btnPage','" + AllPage + "')\" style=\"text-decoration:none;\"> 尾页&nbsp;</a>");
            }
            else if(AllPage!=0)
            {
                sb.Append("<a href=\"javascript:void(0);\" style=\"text-decoration:none;\">下一页&nbsp;</a>");
                sb.Append("<a href=\"javascript:void(0);\" style=\"text-decoration:none;\">尾页&nbsp;</a>");
            }
            CenterPage.Text = sb.ToString();
        }
    }
}